const path = require('path');
const ExtractPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const htmlPlugin = require('html-webpack-plugin');
module.exports = {
    entry:'./src/js/index.js',
    output:{
        path:path.resolve(__dirname,'./dist'),
        filename:'./js/index.js'
    },
    module:{
        rules:[
            {
                test:/\.scss$/,
                use:ExtractPlugin.extract({
                    fallback:'style-loader',
                    use:['css-loader','sass-loader']
                })
            },
            {
                test:/\.js$/,
                use:'babel-loader'
            },
            {
                test:/\.(png|jpg|svg|gif|eot|svg|ttf|woff|woff2)\??.*/,
                use:{
                    loader:'url-loader',
                    options:{
                        limit:1000,
                        name:'imgs/[name].[ext]',
                        publicPath:'/'
                    }
                }
            }
        ]
    },
    plugins:[
        new ExtractPlugin({
            filename:'./css/index.css'
        }),
        new webpack.HotModuleReplacementPlugin(),
        new htmlPlugin({
            filename:'index.html',
            template:'./src/index.html'
        })
    ],
    devServer:{
        port:9090,
        hot:true,
        before(app){
            app.get('/api/list',(req,res,next) => {
                const data = require('./mock/data.json');
                res.end(JSON.stringify({code:1,data:data}))
            })
        }
    },
    devtool:'cheap-module-eval-source-map'
}